﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;

namespace YArchitech.HVAC
{
	public class RisePipeSystem
	{
		public RisePipeSystem(int num, int type = 0)
		{
			this.index = num;
			this.connType = type;
			this.floorSystems = new List<FloorSystem>();
			this.supplyPipes = new PipeSet();
			this.backPipes = new PipeSet();
		}

		public int Index
		{
			get
			{
				return this.index;
			}
			set
			{
				this.index = value;
			}
		}

		public string Name
		{
			get
			{
				return "共用立管" + this.index;
			}
		}

		public int ConnectType
		{
			get
			{
				return this.connType;
			}
			set
			{
				this.connType = value;
			}
		}

		public IList<FloorSystem> FloorSystems
		{
			get
			{
				return this.floorSystems;
			}
		}

		public PipeSet SupplyPipes
		{
			get
			{
				return this.supplyPipes;
			}
		}

		public PipeSet BackPipes
		{
			get
			{
				return this.backPipes;
			}
		}

		public double Resistance
		{
			get
			{
				return this.resistance;
			}
		}

		public double RateUnbalance { get; set; }

		public double PressAcutal { get; set; }

		public int WorstIndex
		{
			get
			{
				return this.GetWorsfloorIndex();
			}
		}

		public void Add(HyCalcPipe pipe, bool bSupply = false)
		{
			if (bSupply)
			{
				if (!this.Contains(this.supplyPipes, pipe))
				{
					this.supplyPipes.Add(pipe);
					return;
				}
			}
			else if (!this.Contains(this.backPipes, pipe))
			{
				this.backPipes.Add(pipe);
			}
		}

		public void Add(FloorSystem floor)
		{
			this.floorSystems.Add(floor);
		}

		private bool Contains(IList<HyCalcPipe> pipes, HyCalcPipe pipe)
		{
			List<ElementId> ids = new List<ElementId>();
			foreach (HyCalcPipe hyCalcPipe in pipes)
			{
				ids.AddRange(from elem in hyCalcPipe.Elements
				select elem.Id);
			}
			return pipe.Elements.Any((Element elem) => ids.Contains(elem.Id));
		}

		public Dictionary<string, double> CalcUnbalance()
		{
			Dictionary<string, double> dictionary = new Dictionary<string, double>();
			foreach (FloorSystem floorSystem in this.floorSystems)
			{
				floorSystem.CalcUnbalance();
				double num = Math.Abs((floorSystem.PressActual - this.resistance) / this.resistance);
				floorSystem.RateUnbalance = num;
				dictionary.Add(floorSystem.Name, num);
				Log.Info(new string[]
				{
					string.Format("floor {0} Resistance = {1} Unbalance={2}", floorSystem.Name, floorSystem.Resistance, num)
				});
			}
			return dictionary;
		}

		public FloorSystem GetAt(int floorIndex)
		{
			if (floorIndex > this.floorSystems.Count)
			{
				return null;
			}
			return (from floor in this.floorSystems
			where floor.Index == floorIndex
			select floor).First<FloorSystem>();
		}

		public double CalcPressCutal()
		{
			for (int i = 0; i < this.floorSystems.Count; i++)
			{
				FloorSystem at = this.GetAt(i + 1);
				if (at != null)
				{
					at.PressActual = at.CalcResistance();
					at.PressActual += this.supplyPipes.SumResistance(i + 1);
					if (this.connType == 0)
					{
						at.PressActual += this.backPipes.SumResistance(this.floorSystems.Count - i);
					}
					else
					{
						at.PressActual += this.backPipes.SumResistance(i + 1);
					}
				}
			}
			this.resistance = (from floor in this.floorSystems
			select floor.PressActual).Max();
			Log.Info(new string[]
			{
				this.Name + ": resistance = " + this.resistance
			});
			return this.resistance;
		}

		private int GetWorsfloorIndex()
		{
			this.worstIndex = (from floor in this.floorSystems
			where floor.PressActual == (from p in this.floorSystems
			select p.PressActual).Max()
			select floor.Index).First<int>();
			return this.worstIndex;
		}

		public TreeNode ToTreeNode()
		{
			TreeNode treeNode = new TreeNode(this.Name);
			treeNode.Tag = this;
			TreeNode treeNode2 = new TreeNode("供水立管");
			treeNode2.Tag = this.SupplyPipes;
			TreeNode treeNode3 = new TreeNode("回水立管");
			treeNode3.Tag = this.BackPipes;
			treeNode.Nodes.AddRange(new TreeNode[]
			{
				treeNode2,
				treeNode3
			});
			treeNode.Nodes.AddRange((from floor in this.floorSystems
			select floor.ToTreeNode()).ToArray<TreeNode>());
			return treeNode;
		}

		public void UpDate()
		{
			string str = string.Empty;
			this.supplyPipes.ForEach(delegate(HyCalcPipe p)
			{
				str = str + p.Name + "  ";
			});
			this.BackPipes.ForEach(delegate(HyCalcPipe p)
			{
				str = str + p.Name + "  ";
			});
			Log.Info(new string[]
			{
				this.Name + " 的立管 : " + str
			});
			this.supplyPipes.UpDate();
			this.backPipes.UpDate();
			this.floorSystems.ForEach(delegate(FloorSystem floor)
			{
				floor.UpDate();
			});
		}

		public void BackRevit()
		{
			this.supplyPipes.BackRevit();
			this.backPipes.BackRevit();
			this.floorSystems.ForEach(delegate(FloorSystem floor)
			{
				floor.BackRevit();
			});
		}

		private int index = 1;

		private PipeSet supplyPipes;

		private PipeSet backPipes;

		private IList<FloorSystem> floorSystems;

		private int connType = 1;

		private int worstIndex = 1;

		private double resistance;
	}
}
